﻿/*
   Author: Tomer Paz
   games.com
   2007
*/

/* Game board views Collection class */

// responsible to store the list of boards Views and once game board collection is being updated
// the game-board-views-collection View is notified and Draws the Active board and Removes the 'passive' boards
// Hence this class is both a collection of Views and a View of other Data-collection
// also a Singleton.


package monopoly.views {
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
    import fl.controls.Label;
	import flash.text.TextFieldAutoSize;
	import flash.display.BlendMode;
	import flash.events.Event;
	import monopoly.IFS.iterators.IIterator;
	import monopoly.IFS.iterators.ArrayIterator;
	import monopoly.IFS.Collections.ICollection;
	import monopoly.views.GameBoardView;
	import monopoly.GameBoard.GameBoardCollection;
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarMode;
	
	public class GameBoardViewCollection extends Sprite implements ICollection
		{
		// for Singelton implementation:
		private static var _instance:GameBoardViewCollection;
		
		private var _GameBoardViews:Array;
		// our 'data':
		private var _GameBoardCollection:GameBoardCollection;
		private var _NumOfLoadedViews:uint;
		// some GUI setup - active board view controller
		private var _ActiveLabel:Label;
		
		/* add progress bar */
		
		private var _progressLabel:Label;
		private var _progressBar:ProgressBar;
		
		
		public function GameBoardViewCollection(parameter:SingletonEnforcer)
			{
			}
			
		public static function getInstance():GameBoardViewCollection
			{
	        if(_instance == null) 
				{
	            _instance = new GameBoardViewCollection(new SingletonEnforcer());
				_instance._GameBoardViews 	= new Array();
				_instance._NumOfLoadedViews = 0;	
							
				_instance.setupLabel("");
				}
				
	        return GameBoardViewCollection._instance;
	      	}
			
		public function addBoardView(value:GameBoardView):void
			{
			_instance._GameBoardViews.push(value);
			
			/* game controller need to know when board finished loading images */
			
			value.addEventListener(Event.COMPLETE, viewCompleteHandler);
			}
			
		public function iterator(type:String = null):IIterator 
			{
		    return new ArrayIterator(_instance._GameBoardViews);
			}
		
		/* now take care of Model-View behavior. "this" VS GameBoardCollection */
		
		public function set GameBoardsCollection(BoardsCollection:GameBoardCollection):void
			{
			_instance._GameBoardCollection = BoardsCollection;
			
			/* we know that this function shall be called only once collection is complete!
			so draw progress bar and update every time a BoardData image loading is done */
			
			_instance.loadProgressBar();
			
			_instance._GameBoardCollection.addEventListener(Event.CHANGE, draw);
			}
			
		public function get GameBoardsCollection():GameBoardCollection
			{
			return _instance._GameBoardCollection;
			}

		/* this is the main logic of this class add/un-visible sprite childs = board views */
		
		private function draw(event:Event):void	
			{
			this.blendMode = BlendMode.LAYER;
			
			// access relevant board View object and make it active -
			// disable (de-activate) Previous board view if existed

			if (_instance.numChildren > 0)		// > 0 i.e. there is a child
				{
				var IsDirectChild:DisplayObject = _instance.getChildAt(0);
			
				if ( IsDirectChild is GameBoardView)
					{
					// make previous board View of this container display tree - "invisible"
					_instance.removeChildAt(0);
					//IsDirectChild.visible = false;
					}
				}
				
			// need to find out which is the active board right now so we know which View to activate (visible)
			_instance.addChildAt(_GameBoardViews[_instance._GameBoardCollection.ActiveBoardIndex], 0);
			//_instance.getChildAt(0).visible = true;
			
			var sprite:Sprite = _instance.getChildAt(0) as Sprite;

			/* show label */
			
			_instance._ActiveLabel.move(0, 450);
			_instance._ActiveLabel.text    = "board: " + _instance._GameBoardCollection.ActiveBoardIndex;
			_instance._ActiveLabel.blendMode = BlendMode.NORMAL;
			_instance._ActiveLabel.visible = true;
			}
		
		/* this function is valuable only during objects Construction */
		
		private function viewCompleteHandler(e:Event):void
			{		
			_instance._NumOfLoadedViews += 1;
			
			_instance._progressBar.setProgress(_instance._progressBar.value + 1, _instance._progressBar.maximum);
			_instance._progressLabel.text = "Loading... " + int(_instance._progressBar.value) + " of " + int(_instance._progressBar.maximum) + " (" + int(_instance._progressBar.percentComplete) + "%)";
			
			/* we care only when All board views are loaded and if we already attached to board collection ! */
			
			if (null != _instance._GameBoardCollection)
				{
				if (_instance._NumOfLoadedViews == _instance._GameBoardCollection.length)
					{
					_instance.removeChild(_instance._progressBar);
					_instance.removeChild(_instance._progressLabel);

					dispatchEvent(new Event(Event.COMPLETE));
					}
				}
			else
				{ /* it does not matter... */
				return;
				}
			}
			
		private function setupLabel(tlabel:String):void
			{
			_instance._ActiveLabel = new Label();
            _instance._ActiveLabel.text = tlabel;
            _instance._ActiveLabel.autoSize = TextFieldAutoSize.LEFT;
			_instance._ActiveLabel.visible = false;
			_instance._ActiveLabel.cacheAsBitmap = true;
			_instance._ActiveLabel.opaqueBackground = 0xFF0000;
			_instance._ActiveLabel.alpha = 20;
			
			_instance.addChild(_instance._ActiveLabel);
			}
			
		private function loadProgressBar():void
			{
			_instance._progressBar	  	 	 = new ProgressBar();
			_instance._progressBar.move(250, 270);
			_instance._progressBar.width	 = 150;
			_instance._progressBar.height	 = 30;
			
			_instance._progressBar.mode 	 = ProgressBarMode.MANUAL;
			_instance._progressBar.minimum   = 0;
			_instance._progressBar.maximum	 = _instance._GameBoardCollection.length;
			_instance._progressBar.value	 = 0;
			_instance._progressBar.visible	 = true;
			
			_instance.addChild(_instance._progressBar);
			
			_instance._progressLabel	 	 = new Label();
			_instance._progressLabel.autoSize = TextFieldAutoSize.LEFT;
			_instance._progressLabel.text 	 = "Start Loading...";
			_instance._progressLabel.move(_instance._progressBar.x, _instance._progressBar.y + _instance._progressBar.height);
			
			_instance.addChild(_instance._progressLabel);
			}
		}
}

class SingletonEnforcer { }